
import { EventConst, Singleton } from "./Common";
import { App } from "../App";
import { isValid, Label, RichText, Sprite, SpriteFrame ,Component, JsonAsset } from "cc";


/**
 * 编辑器调用，动态改变编辑状态显示
 */

export function initEditor(language: string){
    App.i18n.initEditor(language);
}

export class i18nMgr extends Singleton<i18nMgr>{
    private curLang:string = '';     // 当前语言
    private curBundle:string = '';   // 当前子包
    private textComponent:Component[] = [];
    private imgComponent:Component[] = [];
    private textConfig:any = {};   // 文字配置
    private imgConfig:any = {};   // 纹理配置

    /**
     * 仅供cocos creator编辑器调用，项目内部不能使用
     * @param language 
     * @returns 
     */
    public initEditor(language: string) {
        if (language === this.curLang) return ;
        this.curLang = language || 'zh';
        App.Asset.load(`i18n/string`,(cfg:JsonAsset)=>{
            this.textConfig = cfg.json;
            this.textComponent.forEach((it)=>{
                const tmp = it.getComponent('i18nLabel') || it.getComponent('i18nRichText');
                // @ts-ignore
                isValid(tmp) && tmp.reset();
            });
        },JsonAsset);
        App.Asset.load(`i18n/sprite`,(cfg:JsonAsset)=>{
            this.imgConfig = cfg.json;
            this.imgComponent.forEach((it)=>{
                const tmp = it.getComponent('i18nSprite');
                // @ts-ignore
                isValid(tmp) && tmp.reset();
            });
        },JsonAsset);
    }

    protected onInit(){
        App.Asset.load('i18n/string',(cfg:JsonAsset)=>{
            this.textConfig = cfg.json;
        },JsonAsset);
        App.Asset.load('i18n/sprite',(cfg:JsonAsset)=>{
            this.imgConfig = cfg.json;
        },JsonAsset);
        this.language = App.language;
    }

    public set language(lang:string){
        if (this.curLang === lang) {
            return;
        }

        this.curLang = lang;
        for (let it of this.textComponent){
            const tmp = it.getComponent('i18nLabel');
            // @ts-ignore
            isValid(tmp) && tmp.reset();
        }
        for (let it of this.imgComponent){
            const tmp = it.getComponent('i18nSprite');
            // @ts-ignore
            isValid(tmp) && tmp.reset();
        }
        App.Event.emit(EventConst.APP_CHANGE_I18N);
    }

    public register(target:Component){
        if (!isValid(target)){
            return ;
        }

        const arr:Component[] = target instanceof Sprite ? this.imgComponent : this.textComponent;
        arr.indexOf(target) === -1 && arr.push(target);
    }

    public unregister(target:Component){
        if (!isValid(target)){
            return ;
        }

        const arr:Component[] = target instanceof Sprite ? this.imgComponent : this.textComponent;
        let idx = arr.indexOf(target);
        idx !== -1 && arr.splice(idx,1);
    }

    public getString(key:string, ...args: any):string{
        if (this.curLang === '' || !isValid(this.textConfig)){
            this.language = 'zh';
            return 
        }
        const obj = this.textConfig[key];
        if (isValid(obj)){
            const text:string = obj[this.curLang];
            if (isValid(text)){
                return App.Util.format(text,args);
            }
        }
        console.warn(`i18nMgr.getString(${key}) is error.`);
    }

    public setText(component:Label|RichText,key:string,...args: any){
        component && (component.string = this.getString(key,args));
    }

    public  getSprite(key: string, cb: (spriteFrame: SpriteFrame) => void) {
        const obj = this.imgConfig[key];
        if (isValid(obj)){
            App.Asset.load(`i18n/${this.curLang}/${key}`, (spriteFrame: SpriteFrame) => {
                cb(spriteFrame);
            },SpriteFrame);
        }
    }
}
